home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / cops / cops_104 / src / filewriters.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-10  |  2.4 KB  |  121 lines

  1. /* Copyright 1985 Robert W. Baldwin */
  2. /* Copyright 1986 Robert W. Baldwin */
  3.  
  4. /*
  5.    August 15, 1989: Dan Farmer
  6.    One line changed -- #38 is the old line, #41 is my version.
  7.    See comment for details...
  8. */
  9. static    char    *notice85 = "Copyright 1985 Robert W. Baldwin";
  10. static    char    *notice86 = "Copyright 1986 Robert W. Baldwin";
  11.  
  12. /*
  13.  * Useage: filewriters pathname
  14.  * Writes on stdout the list of people who can write the file.
  15.  * This writer's list contains three tokens, the owner, the group, and
  16.  * the 'all others' group, respectively.
  17.  * If either group does not have write access, then that token is
  18.  * replace with the token "NONE".  If the 'all others' group has
  19.  * write access then that token is "OTHER".
  20.  *
  21.  * Notice that the owner of a file can always write it because the
  22.  * owner can change the file access mode.
  23.  *
  24.  * BUG: should handle links correctly.
  25.  */
  26.  
  27. #include    <stdio.h>
  28. #include    <sys/types.h>
  29. #include    <sys/stat.h>
  30. #include    <grp.h>
  31. #include    <pwd.h>
  32.  
  33. /*
  34.   changed this line from upper to lower case 's'.
  35.   Ultrix barfed 'cause already defined in sys/stat.h,
  36.   no one else seemed to mind...
  37.  
  38. #define    S_GWRITE    (S_IWRITE >> 3)
  39. */
  40.  
  41. #ifdef cray
  42. struct group *getgrgid();
  43. #endif
  44.  
  45. #define    s_GWRITE    (S_IWRITE >> 3)        /* Group write access. */
  46. #define    S_OWRITE    (S_IWRITE >> 6)        /* Other write access. */
  47.  
  48.  
  49. main(argc, argv)
  50. int    argc;
  51. char    *argv[];
  52. {
  53.     int    i;
  54.     struct    stat    buf;
  55.  
  56. /*
  57.  * Make sure the file exists.
  58.  */
  59.      if (argc != 2)  {
  60.         fprintf(stderr, "%s: wrong number of args.\n", argv[0]);
  61.         exit(1);
  62.         }
  63.     if (stat(argv[1], &buf) != 0)  {
  64.         fprintf(stderr, "%s: File %s does not exist.\n",
  65.             argv[0], argv[1]);
  66.         exit(1);
  67.         }
  68. /*
  69.  * Produce list of writers.
  70.  * Owner can always write.
  71.  */
  72.     printf("        ");
  73.     print_uid(stdout, buf.st_uid);
  74.     printf(" ");
  75.     if (s_GWRITE & (buf.st_mode))  {
  76.         print_gid(stdout, buf.st_gid);
  77.         }
  78.     else {
  79.         printf("NONE");
  80.         }
  81.     printf(" ");
  82.     if (S_OWRITE & buf.st_mode)  {
  83.         printf("OTHER");
  84.         }
  85.     else {
  86.         printf("NONE");
  87.         }
  88.     printf("\n");
  89.     exit(0);
  90. }
  91.  
  92.  
  93. print_uid(out, uid)
  94. FILE    *out;
  95. int    uid;
  96. {
  97.     struct    passwd    *pwent;
  98.     
  99.     if ((pwent = getpwuid(uid)) == NULL)  {
  100.         fprintf(stderr, "Bad user id %d.\n", uid);
  101.         exit(1);
  102.         }
  103.     fprintf(out, "%s", pwent->pw_name);
  104. }
  105.  
  106.  
  107. print_gid(out, gid)
  108. FILE    *out;
  109. int    gid;
  110. {
  111.     struct    group    *grpent;
  112.     
  113.     if ((grpent = getgrgid(gid)) == NULL)  {
  114.         fprintf(stderr, "Bad group id %d.\n", gid);
  115.         exit(1);
  116.         }
  117.     fprintf(out, "%s", grpent->gr_name);
  118.  
  119. }
  120.  
  121.